class Solution:
    def countDigitOne(self, n: int) -> int:
        # count = 0
        # for i in range(1, n+1):
        #     count += str(i).count('1')
        # return count
        count = 0
        digit = 1  # 从个位开始
        high = n // 10
        cur = n % 10
        low = 0

        while high != 0 or cur != 0:
            if cur == 0:
                count += high * digit
            elif cur == 1:
                count += high * digit + low + 1
            else:
                count += (high + 1) * digit

            # 更新 low, cur, high, digit
            low += cur * digit
            cur = high % 10
            high = high // 10
            digit *= 10

        return count
if __name__ == '__main__':
    s = Solution()
    print(s.countDigitOne(10))